package fuse.LaCollaFS.Api;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.rmi.RemoteException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import LaColla.Api.ApplicationsSideApi;
import LaColla.core.data.Event;
import LaColla.core.data.GroupInfo;
import LaColla.core.util.Debug;

public class ApiImpl implements ApplicationsSideApi {

	private String memberId;

	//private String treeObjId = null;
	
	private String TreeXML = "";

	public ApiImpl() throws RemoteException {
		super();
	}

	/**
	 * 
	 */

	public void newConnectedMember(String groupId, String userId,
			String memberId) throws RemoteException {
	//	p("aaaaaaaaaaaaaaaaaaaaanewConnectedMember " + groupId + " " + userId + " "+ memberId);
		this.memberId = memberId;
	}

	public void memberDisconnected(String groupId, String userId)
			throws RemoteException {
	//	p("memberDisconnected " + groupId + " " + userId);

	}

	public void newEvent(String groupId, LaColla.core.data.app.Event evt)
			throws RemoteException {
	//	p("holaaaaaaaaa!!!!!");
	//	p("-a-newEvent " + evt);

	}

	public void newInfoGroup(String userId, String groupId, String aplicId,
			GroupInfo info) throws RemoteException {
		Debug.sayErr("newInfoGroup", "" + info);
	}

	public void exception(String groupId, String message)
			throws RemoteException {
		// TODO Auto-generated method stub

	}

	public void notifyStopTask(String groupId, String idTask, String result)
			throws RemoteException {
		// TODO Auto-generated method stub

	}

	public void notifyTaskState(String groupId, String idTask, String state,
			String result) throws RemoteException {
		// TODO Auto-generated method stub

	}

	public boolean AppIsAlive(String appId) throws RemoteException {
		// TODO Auto-generated method stub
		return true;
	}

	public void newInstantMsg(String groupId, String userId,
			String destMemberId, Object message) throws RemoteException {
		
		p("newInstantMsg " + groupId + " " + userId + " "
				+ message);

	}

	public void newEvent(String groupId, Event evt) throws RemoteException {
		p("event lacolla.core.data.event!!!!!!!!!!!!");
		p("newEvent " + evt);
	//	p("tipus d'event..."+evt.getDesciptiveTypeOfEvent());
	/*	if (evt.getDesciptiveTypeOfEvent().equals("EventNewObject")) {
			//event New Object!!!
			
			if (ObtenirNomFitxer(evt.getEvent()).equals("dir.xml")) {
				
				p("Error aixo no hauria d'apareixe... ");
				
				//treeObjId = "object#"+ss[1];
			//	p("ja ta....... ");

			}
			
		}
		else */
		
		if (evt.getDesciptiveTypeOfEvent().equals("eventApplication")){ 
		//	p("es un event aplicattion");
		//	p("event "+evt.getEvent());
			p("event rebut!!!!!!!!!!!!!!!!!!!!!");
			TreeXML = evt.getEvent();
		}
		else 
			p("tipus d'event no controlat!!!!!!!");
	}

	private void p(Object o) {
		System.out.println(o);
		
	}

	/*	private String ObtenirNomFitxer(String s) {
		String[] ss = s.split("<");
		String r = "";
		for (int i = 1; i < ss.length; i++) {
			r = r + "<" + ss[i];
		}

		Reader stringReader = new StringReader(r);
		SAXBuilder builder = new SAXBuilder();
		Document doc = null;
		try {
			doc = builder.build(stringReader);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Element root = doc.getRootElement();
		List l = root.getChildren();
		Element filename = (Element) l.get(0);
		String result = filename.getValue();
		return result;
	}*/

	/*public String gettreeObjId() {
		return treeObjId;
	}*/

	public String getMemberId() {
		// TODO Auto-generated method stub
		return memberId;
	}

	public void initialize(Object obj) throws java.rmi.RemoteException {

	}

	public String getTree() {
		// TODO Auto-generated method stub
		return TreeXML;
	}

	public void setTree(String arbrexml) {
		TreeXML = arbrexml;
		
	}

}
